package io.renren.modules.alarm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.alarm.dao.AlarmSensorDao;
import io.renren.modules.alarm.entity.AlarmSensorEntity;
import io.renren.modules.alarm.entity.AlarmSensorVoEntity;
import io.renren.modules.alarm.service.AlarmSensorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AlarmSensorServiceImpl implements AlarmSensorService {

    @Resource
    private AlarmSensorDao alarmSensorDao;

    //获取设备集合以及设备下传感器集合
    @Override
    public PageUtils getList(Map<String, Object> params) {
        IPage<AlarmSensorVoEntity> page = alarmSensorDao.getDeviceList(new Query<AlarmSensorVoEntity>().getPage(params), params);
        List<AlarmSensorVoEntity> records = page.getRecords();
        for (AlarmSensorVoEntity alarmSensorVo : records) {
            alarmSensorVo.setAlarmSensorList(alarmSensorDao.getList(alarmSensorVo.getDeviceId()));
        }
        return new PageUtils(page);
    }

    @Override
    public R get(Integer deviceId) {
        //获取设备信息
        AlarmSensorVoEntity device = alarmSensorDao.getDevice(deviceId);
        //获取设备下的传感器集合
        device.setAlarmSensorList(alarmSensorDao.getList(deviceId));
        return R.ok(device);
    }

    //新增
    @Override
    public R add(AlarmSensorVoEntity alarmSensorVo) {
        //只处理传感器的数据
        List<AlarmSensorEntity> alarmSensorList = alarmSensorVo.getAlarmSensorList();
        alarmSensorDao.add(alarmSensorList);
        return R.ok("新增成功");
    }

    //修改
    @Override
    public R update(AlarmSensorVoEntity alarmSensorVo) {
        List<AlarmSensorEntity> alarmSensorList = alarmSensorVo.getAlarmSensorList();
        //要修改哪个传感器的数据就传递哪个传感器的数据
        //新增后无法通过修改增加或删除传感器的个数
        for (AlarmSensorEntity alarmSensor : alarmSensorList) {
            alarmSensorDao.update(alarmSensor);
        }
        return R.ok("修改成功");
    }

    @Override
    public R delete(Integer sensorId) {
        alarmSensorDao.delete(sensorId);
        return R.ok("删除成功");
    }

    @Override
    public R deleteSensor(Integer deviceId) {
        alarmSensorDao.deleteSensor(deviceId);
        return R.ok("删除成功");
    }


}
